//
//  YahooStockExchangeOperatpr.m
//  MarketDataColloctor
//z
//  Created by jiro on 10/03/10.
//  Copyright 2010 jiro music. All rights reserved.
//

#import "YahooStockExchangeHtmlOperator.h"
#import "YahooStockExchangeHtmlCollector.h"
#import "YahooStockExchangeHtmlInterpreter.h"
#import "../LocalFilePath.h"
#import "../../../DbTypes/DbTypes.h"
#import "../../../Commons/DbOperator.h"

@implementation YahooStockExchangeHtmlOperator

- (id) initWithFileSaveFolder: (NSString*) folderPath
{
	self = [ super initWithSaveFolder: folderPath ];
	collector = [ [ YahooStockExchangeHtmlCollector alloc ] init ];
	interpreter = [ [ YahooStockExchangeHtmlInterpreter alloc ] init ]; 
	marketDataType = MarketData::Type_StockExchangeJa;
	return self;
}

- (NSArray*) createInsertSqlStatements: (DbTableEntryList*) list 
	toDate: (NSDate*) latestDate 
	lastDate: (NSDate**) lastDate
{
	NSMutableString* exchangeSql = [ NSMutableString stringWithFormat: @"insert into %@ values\n  ", [ StockExchangeJa tableName ] ];
	NSMutableString* divisionSql = [ NSMutableString stringWithFormat: @"insert into %@ values\n  ", [ StockUnitDivitionJa tableName ] ];
	Class exchangeClass = [ [ [ StockExchangeJa alloc ] init ] class ];
	Class divisionClass = [ [ [ StockUnitDivitionJa alloc ] init ] class ];
	
	*lastDate = nil;
	NSEnumerator* e = [ list objectEnumerator ];
	id entry = [ list objectAtIndex: 0 ];
	BOOL isExchangeValueAdded = NO;
	BOOL isDivisionValueAdded = NO;
	while ( entry = [ e nextObject ] )
	{
		if ( [ entry class ] == exchangeClass )
		{
			StockExchangeJa* exchange = (StockExchangeJa*) entry;
			if ( [ exchange.exchangeDate timeIntervalSinceDate: latestDate ] <= 0 )
			{
				continue;
			}

			if ( NO == isExchangeValueAdded )
			{
				if ( * lastDate == nil )
				{
					*lastDate = exchange.exchangeDate;
				}
				isExchangeValueAdded = YES;
				[ exchangeSql appendString: [ exchange queryInsertValue ] ];
			}
			else
			{
				if ( [ *lastDate timeIntervalSinceDate: exchange.exchangeDate ] < 0 )
				{
					*lastDate = exchange.exchangeDate;
				}				[ exchangeSql appendString: @", " ];
				[ exchangeSql appendString: [ exchange queryInsertValue ] ];
			}

			[ exchangeSql appendString: @"\n" ];
		}
		else if ( [ entry class ] == divisionClass )
		{
			StockUnitDivitionJa* division = (StockUnitDivitionJa*) entry;
			if ( [ division.divisionDate timeIntervalSinceDate: latestDate ] <= 0 )
			{
				continue;
			}

			if ( NO == isDivisionValueAdded )
			{
				if ( * lastDate == nil )
				{
					*lastDate = division.divisionDate;
				}
				isDivisionValueAdded = YES;
				[ divisionSql appendString: [ division queryInsertValue ] ];
			}
			else
			{
				if ( [ *lastDate timeIntervalSinceDate: division.divisionDate ] < 0 )
				{
					*lastDate = division.divisionDate;
				}
				[ divisionSql appendString: @", " ];
				[ divisionSql appendString: [ division queryInsertValue ] ];
			}
			[ divisionSql appendString: @"\n" ];
		}
	}
	
	NSMutableArray* a = [ NSMutableArray array ];
	if ( YES == isExchangeValueAdded )
	{
		[ exchangeSql appendString: @";\n" ];
		[ a addObject:exchangeSql ];
	}
	if ( YES == isDivisionValueAdded )
	{
		[ divisionSql appendString: @";\n" ];
		[ a addObject:divisionSql ];
	}
	return a;
}

@end
